package com.xhs.common.util.json;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.xhs.common.bean.XhsAccessToken;

import java.lang.reflect.Type;

public class XhsAccessTokenAdapter implements JsonDeserializer<XhsAccessToken> {

  @Override
  public XhsAccessToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    XhsAccessToken accessToken = new XhsAccessToken();
    JsonObject accessTokenJsonObject = json.getAsJsonObject();

    if (accessTokenJsonObject.get("access_token") != null && !accessTokenJsonObject.get("access_token").isJsonNull()) {
      accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
    }
    if (accessTokenJsonObject.get("expire_in") != null && !accessTokenJsonObject.get("expire_in").isJsonNull()) {
      accessToken.setExpireIn(GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expire_in")));
    }
    return accessToken;
  }

}
